import {defineStore} from 'pinia'
import {Names} from "./store-name";

type User = {
    name: string,
    age: number
}

let result: User = {
    name: '飞机',
    age: 20
}

const Login = (): Promise<User> => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                name: '飞机',
                age: 20
            })
        }, 2000)
    })
}

export const useTestStore = defineStore(Names.TEST, {
    state: () => {
        return {
            user: <User>{},
            name: '董咚懂咚'
        }
    },
    //computed 修饰一些值
    getters: {
        newName(): string {
            return `$-${this.name}-${this.getUserAge}`
        },
        getUserAge(): number {
            return this.user.age
        }
    },
    //methods 可以做同步、异步，提交state
    actions: {
        async setUser() {
            // this.user = result
            const result = await Login()
            this.user = result
            this.setName(result.name)
        },
        setName(name: string) {
            this.name = name
        }
    }
})